using System;
using System.Collections.Generic;
using System.Data;

namespace ApplicationTemplate
{
    class CreateDamUpdateStatus2API
    {
        public static String CreateUpdateStatus(String realTableName, DataSet FieldsDs)
        {
            String tableName = realTableName.Replace("_", "").Replace("tb", "");
            String method = String.Format(
@"public int Update{0}RecordStatus({1}DBManager dbManager)
        {{
            AppContext tac = AppContext.Current;

            string sqlString = string.Format(@""
                UPDATE {{0}}
                SET
{2}
                WHERE
{3}
            "", this.{4});

            DbCommand command = GetSqlStringCommand(sqlString);

            //パラメータセット
{5}
            //SQL文を実行する
            return ExecuteNonQuery(command, dbManager);
        }}
",
            tableName,
            SQLClass.GetPkParams(FieldsDs),
            GetUpdateStatusFields(FieldsDs),
            GetUpdateStatusFieldsWhere(FieldsDs),
            realTableName,
            GetUpdateStatusFieldsBind(FieldsDs));

            String pkComment = SQLClass.GetPkParamsComment(FieldsDs);

            String comment = String.Format(@"
        /// <summary>
        /// 削除処理
        /// </summary>
{0}
        /// <param name=""dbManager"">データベースマネージャ</param>
        /// <returns>処理結果</returns>
        {1}", pkComment, method);

            return comment;
        }

        private static String GetUpdateStatusFields(DataSet ds)
        {
            String fields = string.Empty;
            for (int i = 0; i < ds.Tables[0].Rows.Count; i++)
            {
                DataRow row = ds.Tables[0].Rows[i];
                String user_type_id = row["user_type_id"].ToString();
                String name = row["name"].ToString();

                if (name == "UpdateUser" || name == "update_user" ||
                    name == "UpdateDate" || name == "update_date" ||
                    name == "RecordStatus" || name == "record_status")
                {
                    if (String.IsNullOrEmpty(fields))
                    {
                        fields += String.Format("                    {0} = @{0}{1}", name, Environment.NewLine);
                    }
                    else
                    {
                        fields += String.Format("                    ,{0} = @{0}{1}", name, Environment.NewLine);
                    }

                }
            }
            fields = fields.TrimEnd(Environment.NewLine.ToCharArray());
            return fields;
        }

        private static String GetUpdateStatusFieldsWhere(DataSet ds)
        {
            String fields = string.Empty;
            for (int i = 0; i < ds.Tables[0].Rows.Count; i++)
            {
                DataRow row = ds.Tables[0].Rows[i];
                String user_type_id = row["user_type_id"].ToString();
                String name = row["name"].ToString();

                if (row["is_pk"].ToString() == "1")
                {
                    if (String.IsNullOrEmpty(fields))
                    {
                        fields += String.Format("                    {0} = @{0}{1}", name, Environment.NewLine);
                    }
                    else
                    {
                        fields += String.Format("                    AND {0} = @{0}{1}", name, Environment.NewLine);
                    }
                }

            }

            fields = fields.TrimEnd(Environment.NewLine.ToCharArray());
            return fields;
        }

        private static String GetUpdateStatusFieldsBind(DataSet ds)
        {
            String fields = string.Empty;
            for (int i = 0; i < ds.Tables[0].Rows.Count; i++)
            {
                DataRow row = ds.Tables[0].Rows[i];
                String user_type_id = row["user_type_id"].ToString();
                String name = row["name"].ToString();

                String type = "String";
                if (user_type_id == "56")
                {
                    type = "Int32";
                }
                else if (user_type_id == "61" || user_type_id == "40")
                {
                    type = "DateTime";
                }

                if (name == "RecordStatus" || name == "record_status")
                {
                    fields += String.Format(@"            AddInParameter(command, ""@{0}"", DbType.Int32, CmnConsts.RECORD_STATUS_DELETE);", name) + Environment.NewLine;
                }

                if (name == "UpdateUser" || name == "update_user")
                {
                    fields += "            AddInParameter(command, \"@" + name + "\", DbType.String, tac.UserDataManager.UserId);" + Environment.NewLine;
                    continue;
                }

                if (name == "UpdateDate" || name == "update_date")
                {
                    fields += "            AddInParameter(command, \"@" + name + "\", DbType.DateTime, tac.UpdateTime);" + Environment.NewLine;
                    continue;
                }


                if (row["is_pk"].ToString() == "1")
                {
                    String paramsName = name.Substring(0, 1).ToLower() + name.Substring(1);
                    fields += String.Format(@"            AddInParameter(command, ""@{0}"", DbType.{1}, {2});", name, type, paramsName) + Environment.NewLine;
                }
            }

            return fields;
        }
    }
}
